package tools.httptools;

import java.lang.management.ManagementFactory;

import com.sun.management.OperatingSystemMXBean;

import lombok.extern.slf4j.Slf4j;

@SuppressWarnings("restriction")
@Slf4j
public class CpuUtils {


    private static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

    public static void main(String[] args) throws InterruptedException {

        while (true) {

            //获取CPU
            double cpuLoad = osmxb.getSystemCpuLoad();
            int percentCpuLoad = (int) (cpuLoad * 100);
            //获取内存
            double totalvirtualMemory = osmxb.getTotalPhysicalMemorySize();
            double freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
            double value = freePhysicalMemorySize / totalvirtualMemory;
            int percentMemoryLoad = (int) ((1 - value) * 100);

            log.info("CPU = {},Mem = {}", percentCpuLoad, percentMemoryLoad);
            Thread.sleep(1000);

        }

    }



}


